using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using SftTreeLib70;
using AxSftTreeLib70;
using Softelvdm.OLEConvert;

namespace Virtual
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        public System.Windows.Forms.Button Command1;
        public System.Windows.Forms.Label Label5;
        public System.Windows.Forms.Timer Timer1;
        private AxSftTreeLib70.AxSftTree axSftTree1;
        private System.Windows.Forms.ImageList imageListWorld;
        private System.Windows.Forms.ImageList imageListBitmaps;
        public System.Windows.Forms.TextBox EditControl;
        private Softelvdm.OCXHelper.SftHelperComponent sftHelperComponent1;
        private System.ComponentModel.IContainer components;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.axSftTree1 = new AxSftTreeLib70.AxSftTree();
            this.EditControl = new System.Windows.Forms.TextBox();
            this.Command1 = new System.Windows.Forms.Button();
            this.Label5 = new System.Windows.Forms.Label();
            this.Timer1 = new System.Windows.Forms.Timer(this.components);
            this.imageListWorld = new System.Windows.Forms.ImageList(this.components);
            this.imageListBitmaps = new System.Windows.Forms.ImageList(this.components);
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).BeginInit();
            this.SuspendLayout();
            // 
            // axSftTree1
            // 
            this.axSftTree1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.axSftTree1.Location = new System.Drawing.Point(8, 8);
            this.axSftTree1.Name = "axSftTree1";
            this.axSftTree1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftTree1.OcxState")));
            this.axSftTree1.Size = new System.Drawing.Size(353, 400);
            this.axSftTree1.TabIndex = 30;
            this.axSftTree1.ItemDblClick += new AxSftTreeLib70._DSftTreeEvents_ItemDblClickEventHandler(this.axSftTree1_ItemDblClick);
            this.axSftTree1.EditEnded += new AxSftTreeLib70._DSftTreeEvents_EditEndedEventHandler(this.axSftTree1_EditEnded);
            this.axSftTree1.EditEnding += new AxSftTreeLib70._DSftTreeEvents_EditEndingEventHandler(this.axSftTree1_EditEnding);
            this.axSftTree1.ItemClick += new AxSftTreeLib70._DSftTreeEvents_ItemClickEventHandler(this.axSftTree1_ItemClick);
            this.axSftTree1.EditInitializing += new AxSftTreeLib70._DSftTreeEvents_EditInitializingEventHandler(this.axSftTree1_EditInitializing);
            this.axSftTree1.VirtualItem += new AxSftTreeLib70._DSftTreeEvents_VirtualItemEventHandler(this.axSftTree1_VirtualItem);
            this.axSftTree1.EditValidate += new AxSftTreeLib70._DSftTreeEvents_EditValidateEventHandler(this.axSftTree1_EditValidate);
            this.axSftTree1.EditNavigating += new AxSftTreeLib70._DSftTreeEvents_EditNavigatingEventHandler(this.axSftTree1_EditNavigating);
            // 
            // EditControl
            // 
            this.EditControl.AcceptsReturn = true;
            this.EditControl.AutoSize = false;
            this.EditControl.BackColor = System.Drawing.SystemColors.Window;
            this.EditControl.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.EditControl.Enabled = false;
            this.EditControl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.EditControl.ForeColor = System.Drawing.SystemColors.WindowText;
            this.EditControl.Location = new System.Drawing.Point(368, 48);
            this.EditControl.MaxLength = 0;
            this.EditControl.Name = "EditControl";
            this.EditControl.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.EditControl.Size = new System.Drawing.Size(89, 19);
            this.EditControl.TabIndex = 33;
            this.EditControl.Text = "EditControl";
            this.EditControl.Visible = false;
            // 
            // Command1
            // 
            this.Command1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
            this.Command1.BackColor = System.Drawing.SystemColors.Control;
            this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Command1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command1.Location = new System.Drawing.Point(368, 8);
            this.Command1.Name = "Command1";
            this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command1.Size = new System.Drawing.Size(88, 24);
            this.Command1.TabIndex = 31;
            this.Command1.Text = "Close";
            this.Command1.Click += new System.EventHandler(this.Command1_Click);
            // 
            // Label5
            // 
            this.Label5.BackColor = System.Drawing.SystemColors.Control;
            this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label5.Location = new System.Drawing.Point(368, 120);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(89, 328);
            this.Label5.TabIndex = 32;
            this.Label5.Text = "Click on the SftTree/OCX control and hit F1 to access online help for SftTree/OCX" +
                " 6.0";
            this.Label5.Visible = false;
            // 
            // Timer1
            // 
            this.Timer1.Enabled = true;
            this.Timer1.Tick += new System.EventHandler(this.Timer1_Tick);
            // 
            // imageListWorld
            // 
            this.imageListWorld.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imageListWorld.ImageSize = new System.Drawing.Size(32, 32);
            this.imageListWorld.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListWorld.ImageStream")));
            this.imageListWorld.TransparentColor = System.Drawing.Color.White;
            // 
            // imageListBitmaps
            // 
            this.imageListBitmaps.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imageListBitmaps.ImageSize = new System.Drawing.Size(16, 16);
            this.imageListBitmaps.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListBitmaps.ImageStream")));
            this.imageListBitmaps.TransparentColor = System.Drawing.Color.Blue;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.Command1;
            this.ClientSize = new System.Drawing.Size(464, 414);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Command1,
                                                                          this.Label5,
                                                                          this.axSftTree1,
                                                                          this.EditControl});
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - Virtual Sample";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).EndInit();
            this.ResumeLayout(false);

        }
		#endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        //--locate-marker--

        public int m_PicCount;  // spinning globe index

        private void Command1_Click(object sender, System.EventArgs e) {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e) {
            m_PicCount = 0;
            axSftTree1.VirtualMode = true;
            axSftTree1.VirtualCount(10000000);
            axSftTree1.VirtualImageSizes(16, 16, 16, 16, 16, 16, 16, 16);
            // Make columns and row headers optimal
            // but do this at the end of the list, because our sample
            // data is larger at the end
            axSftTree1.Items.TopIndex = axSftTree1.Items.Count - 1;
            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.RowHeaders.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
            axSftTree1.Items.TopIndex = 0;
       }

        private void Timer1_Tick(object sender, System.EventArgs e) {
            axSftTree1.RowColumnHeader.Image.NETImageObject = imageListWorld.Images[m_PicCount];
            ++m_PicCount;
            if (m_PicCount >= imageListWorld.Images.Count)
                m_PicCount = 0;
        }

        private void axSftTree1_VirtualItem(object sender, AxSftTreeLib70._DSftTreeEvents_VirtualItemEvent e) {
        
            e.itemObject.Item.get_Cell(0).Text = "Item " + e.rowIndex;
            e.itemObject.Item.get_Cell(1).Text = "Cell " + e.rowIndex.ToString();
            e.itemObject.Item.get_Cell(2).Text = "A";
            e.itemObject.Item.get_Cell(3).Text = (e.rowIndex % 7).ToString();
            e.itemObject.Item.get_Cell(4).Text = "Last " + e.rowIndex.ToString();
            e.itemObject.Item.RowHeader.Text = "R" + e.rowIndex.ToString();
            e.itemObject.Item.Enabled = ((e.rowIndex % 2) == 0);
        
            if (e.rowIndex % 17 == 0)
                e.itemObject.Item.RowHeader.Image.NETImageObject = imageListBitmaps.Images[e.rowIndex % 10];
            if (e.rowIndex % 3 == 0) {
                e.itemObject.Item.get_Cell(1).ForeColor = OLECvt.ToOleColor(System.Drawing.Color.Red);
                e.itemObject.Item.get_Cell(1).BackColor = OLECvt.ToOleColor(System.Drawing.Color.Aqua);
            }
            if (e.rowIndex % 5 == 0)
                e.itemObject.Item.get_Cell(1).Font.Bold = true;
            if (e.rowIndex % 7 == 0) {
                e.itemObject.Item.Image.NETImageObject = imageListBitmaps.Images[(e.rowIndex + 9) % 10];
                e.itemObject.Item.LabelImage.NETImageObject = imageListBitmaps.Images[(e.rowIndex + 1) % 10];
            }
            if (e.rowIndex % 13 == 0)
                e.itemObject.Item.RowHeader.Image.NETImageObject = imageListBitmaps.Images[(e.rowIndex + 5) % 10];
            if (e.rowIndex % 11 == 0)
                e.itemObject.Item.RowHeader.ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            if (e.rowIndex % 9 == 0)
                e.itemObject.Item.get_Cell(1).Image.NETImageObject = imageListBitmaps.Images[(e.rowIndex + 3) % 10];
            if (e.rowIndex % 7 == 0) {
                e.itemObject.Item.get_Cell(0).Image.NETImageObject = imageListBitmaps.Images[(e.rowIndex + 2) % 10];
                e.itemObject.Item.get_Cell(0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            }
        }

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemClickEvent e) {
            if ((SftTreeAreaTypeConstants) e.areaType == SftTreeAreaTypeConstants.constSftTreeText)
                axSftTree1.get_Cell(e.itemIndex, e.colIndex).Edit(0, 0);
        }

        private void axSftTree1_ItemDblClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemDblClickEvent e) {
            if ((SftTreeAreaTypeConstants) e.areaType == SftTreeAreaTypeConstants.constSftTreeColumnRes)
                axSftTree1.get_Column(e.colIndex).MakeOptimal();
        
        }

        private void axSftTree1_EditInitializing(object sender, AxSftTreeLib70._DSftTreeEvents_EditInitializingEvent e) {

            Timer1.Enabled = false; // stop the spinning globe
            
            // LeftPix/TopPix/WidthPix/HeightPix describes the current cell area
            // we need to return the position and size needed for editing.
            // In this example, we use the height of the control on the form
            // and center it over the cell.
            e.topPix = e.topPix + (e.heightPix - EditControl.Height) / 2;
            e.heightPix = EditControl.Height;
            
            // Set the text in the control used for cell editing and
            // set other control-specific properties
            EditControl.Width = 200;
            EditControl.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
            EditControl.SelectionStart = 0;
            EditControl.SelectionLength = 999;
            
            // Return the control's window handle
            e.window = (int) EditControl.Handle;
            e.vData = EditControl;

            // Define navigation keys
            // VK_TAB
            axSftTree1.CellEditIntercept(9, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_RETURN
            axSftTree1.CellEditIntercept(13, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_HOME
            axSftTree1.CellEditIntercept(36, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar);
            // VK_END
            axSftTree1.CellEditIntercept(35, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar);
            // VK_UP
            axSftTree1.CellEditIntercept(38, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_DOWN
            axSftTree1.CellEditIntercept(40, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
        }

        private void axSftTree1_EditNavigating(object sender, AxSftTreeLib70._DSftTreeEvents_EditNavigatingEvent e) {
            // Process key pressed
            axSftTree1.EditNavigate(e.key, e.shift);
        }

        private void axSftTree1_EditValidate(object sender, AxSftTreeLib70._DSftTreeEvents_EditValidateEvent e) {
            // Validate the new cell contents
            if (EditControl.Text != axSftTree1.get_Cell(e.editIndex, e.editCol).Text)
                MessageBox.Show("This example doesn't preserve the changes you make, because the sample data is randomly generated.");
        }

        private void axSftTree1_EditEnding(object sender, AxSftTreeLib70._DSftTreeEvents_EditEndingEvent e) {
            Control ctrl = (Control) e.vData;
            ctrl.Visible = false;
            ctrl.Enabled = false;
        }

        private void axSftTree1_EditEnded(object sender, AxSftTreeLib70._DSftTreeEvents_EditEndedEvent e) {
            Timer1.Enabled = true; // restart the spinning globe
        }

    }
}